xquery version "1.0";

(:~
	Controller XQuery for the Search Engine application.
:)

(: Root path: redirect to medex.xql :)
if ($exist:path eq '/') then
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
		<redirect url="medex.xql"/>
	</dispatch>
(:~ 
	Execute the Search. Get all fields from the form then pass it as parameters.
	engine.xql is pipelined with session.xql. All results from the engine are
	passed to the session for storing. All results are stored in the HTTP
	session. Time is evaluated from the start of the execute, to the time it
	enters to the session for storing.
:)
else if ($exist:resource eq 'search') then
	let $params := request:get-parameter-names()
	let $new := (for $param in $params
					let $val := request:get-parameter("$param",())
					return
						<add-parameter name="{$param}" value="{$val}"/>)
    let $startTime := util:system-time()
    return
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
			<!-- Query is executed by XQueryServlet -->
            <forward url="engine.xql">
				{$new}
			</forward>
			<view>
				<forward url="session.xql">
					<set-attribute name="elapsed" 
						value="{string(seconds-from-duration(util:system-time() - $startTime))}"/>
				</forward>
			</view>
			
        </dispatch>
(: Log in authentication for users:)
else if ($exist:resource eq 'login') then
	let $username := request:get-parameter("user", ()),
		$password := request:get-parameter("pass",())
		return
			<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
				<forward url="login.xql">
					<add-parameter name="user" value="{$username}"/>
					<add-parameter name="pass" value="{$password}"/>
				</forward>
			</dispatch>
(:~
	Record Management
:)
else if($exist:resource eq 'modify') then
	let $command := request:get-parameter("command", ())
	let $id := request:get-parameter("id", ())
	let $xml := request:get-parameter("xml", ())
    return
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
		    <forward url="save.xql">
				<add-parameter name="command" value="{$command}"/>
				<add-parameter name="id" value="{$id}"/>
				<add-parameter name="xml" value="{$xml}"/>
			</forward>			
        </dispatch>			
(:~ 
	Retrieval of Results. 0 if the session will return the table with results
	1-max result if the session will return the nth record in the results (rank)
:)
else if (starts-with($exist:path, '/results/')) then
	<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
		<forward url="../session.xql">
			<add-parameter name="num" value="{$exist:resource}"/>
		</forward>
	</dispatch>
(:~ 
	Retrieval of Format. 0 if the session will return the format
	of the whole record. 1 if the session will return the fields
	that will be displayed in the table results.
:)	
else if (starts-with($exist:path, '/format/')) then
	<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
		<forward url="../session.xql">
			<add-parameter name="format" value="{$exist:resource}"/>
		</forward>
	</dispatch>	
else
    (: everything else is passed through :)
    <ignore xmlns="http://exist.sourceforge.net/NS/exist">
        <cache-control cache="yes"/>
    </ignore>
